package com.ego.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ego.product.po.SkuInfo;
import com.ego.product.service.SkuInfoService;
import com.ego.product.vo.DetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ego.product.dao.SkuSaleAttrValueDao;
import com.ego.product.po.SkuSaleAttrValue;
import com.ego.product.service.SkuSaleAttrValueService;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: ego
 * @description sku销售属性&值对应实现类
 * @author KamTang
 * @date 2022-11-13 12:56:14
 */
@Service("skuSaleAttrValueService")
public class SkuSaleAttrValueServiceImpl extends ServiceImpl<SkuSaleAttrValueDao, SkuSaleAttrValue> implements SkuSaleAttrValueService {

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * SQL语句
     * SELECT
     * 	ssav.attr_id,
     * 	ssav.attr_name,
     * 	GROUP_CONCAT( DISTINCT ssav.attr_value )
     * FROM
     * 	`pms_sku_info` psi
     * 	LEFT JOIN `pms_sku_sale_attr_value` ssav ON ssav.sku_id = psi.sku_id
     * WHERE
     * 	psi.spu_id = #{skuId}
     *
     * GROUP BY
     * 	ssav.attr_id,
     * 	ssav.attr_name
     */
    @Override
    public List<DetailVo.DetailSaleAttrVo> getBySpuId(Long spuId) {
        List<DetailVo.DetailSaleAttrVo> detailSaleAttrs = new ArrayList<>();

        // 获取当前spu中的sku
        List<Long> skuIds = skuInfoService.list(new QueryWrapper<SkuInfo>().eq("spu_id", spuId))
                .stream().map(SkuInfo::getSkuId).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(skuIds)) {
            return detailSaleAttrs;
        }

        // 获取所有sku涉及到的属性组合和attrId
        List<SkuSaleAttrValue> skuSaleAttrValues = this.list(new QueryWrapper<SkuSaleAttrValue>().in("sku_id", skuIds));
        List<Long> attrIds = skuSaleAttrValues.stream().map(SkuSaleAttrValue::getAttrId).distinct().collect(Collectors.toList());

        Map<Long, List<SkuSaleAttrValue>> skuSaleAttrValueMap = skuSaleAttrValues.stream()
                .collect(Collectors.groupingBy(SkuSaleAttrValue::getAttrId, Collectors.toList()));

        detailSaleAttrs = attrIds.stream().map(attrId -> {
            DetailVo.DetailSaleAttrVo detailSaleAttr = new DetailVo.DetailSaleAttrVo();

            Map<String, List<SkuSaleAttrValue>> attrValueMap = skuSaleAttrValueMap.get(attrId)
                    .stream().collect(Collectors.groupingBy(SkuSaleAttrValue::getAttrValue, Collectors.toList()));

            List<DetailVo.DetailSaleAttrVo.AttrValueWithSkuIdVo> attrValues = attrValueMap.keySet().stream().map(value -> {
                DetailVo.DetailSaleAttrVo.AttrValueWithSkuIdVo attrValue = new DetailVo.DetailSaleAttrVo.AttrValueWithSkuIdVo();
                attrValue.build(value, attrValueMap.get(value).stream().map(SkuSaleAttrValue::getSkuId).collect(Collectors.toList()));
                return attrValue;
            }).collect(Collectors.toList());

            detailSaleAttr.build(attrId, skuSaleAttrValueMap.get(attrId).get(0).getAttrName(), attrValues);
            return detailSaleAttr;
        }).collect(Collectors.toList());
        return detailSaleAttrs;
    }

    @Override
    public List<String> gets(Long skuId) {
        List<SkuSaleAttrValue> skuSaleAttrValues = this.baseMapper
                .selectList(new QueryWrapper<SkuSaleAttrValue>().eq("sku_id", skuId));
        return skuSaleAttrValues.stream()
                .map(skuSaleAttrValue -> skuSaleAttrValue.getAttrName() + ":" + skuSaleAttrValue.getAttrValue())
                .collect(Collectors.toList());
    }
}
